package com.game.adapter;
  
import java.util.Calendar;
import java.util.Date;
import java.util.Random;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.Log;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.game.linerunner.R;
   
public class NewGameDraw extends View implements OnClickListener {
   private int xMin = 0;          // This view's bounds
   private int xMax;
   private int yMin = 0;
   private int yMax;
   private float ballRadius = 8; // Ball's radius
   private float ballX = ballRadius;  // Ball's center (x,y)
   private float ballY = ballRadius;
   private float ballSpeedX = 5;  // Ball's speed (x,y)
   private float ballSpeedY = 3;
   private RectF personBounds;      // Needed for Canvas.drawOval
   private Paint paint;           // The paint (e.g. style, color) used for drawing
   
   //used to set the bounds, of the rectangle
   private float personX = 100;
   private float personY = 100;
   private float groundLineX = xMax;
   private float groundLineY = 150;
   private long seconds = 0;
   //buttons
   private Button btnJump;
   private Button btnRoll;
   //Time stuff, measure elapsed time
   Calendar c;
   Date timeStart;
   Date timeEnd;
   Date timeStartTimer;
   
   //physics
   private float YVel = 20,YAcc = 30;
   
   // Constructor
   public NewGameDraw(Context context) {
      super(context);
      personBounds = new RectF();
      paint = new Paint();
      
      btnJump= new Button(context);
      
      btnJump.setText(R.string.buttonJump);
      
      c = Calendar.getInstance(); 
      timeStartTimer = new Date();
      btnJump.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              drawPersonJump();
          }
      });
      
   }
  
   // Called back to draw the view. Also called by invalidate().
   @Override
   protected void onDraw(Canvas canvas) {
	   timeStart = new Date();
//      personBounds.set(personLX, personLY, personRX, personRY);
      
      paint.setColor(Color.GREEN);
//      canvas.drawRect(personBounds, paint);
      drawBullet(canvas, ballX, ballY);
//      drawPerson(canvas, ballX, ballY);
//    drawRandomObstecal(canvas);
      canvas.drawLine(xMin, groundLineY, xMax, groundLineY, paint);
      // Update the position of the ball, including collision detection and reaction.
      update();
  
      // Delay
      try {  
         Thread.sleep(10);  
      } catch (InterruptedException e) { }
      
      invalidate();  // Force a re-draw
   }
   
   // Detect collision and update the position of the ball.
   private void update() {
      // Get new (x,y) position
      seconds +=1;
      groundLineX -= 1;
      
      ballY = ballY + YVel;
	   
	    
     if(groundLineX <= 0){
    	 groundLineX = xMax;
     }
     
    
     
     
     
     
     
     timeEnd = new Date();
     
     float timeElapsed = timeEnd.getTime() - timeStart.getTime();
	 Log.v("LIneRunner", "seconds:  " +seconds);  
     YVel = (float) (YVel + YAcc * 0.166666677);
	   
	   
	   
	   if(ballY >= groundLineY ){
		   YVel = 0;
		   
	   }
	       
	   if(ballX >= groundLineY){
		   YVel = 0;
		   
	   }
	 
   }
   
   // Called back when the view is first created or its size changes.
   @Override
   public void onSizeChanged(int w, int h, int oldW, int oldH) {
      // Set the movement bounds for the ball
      
	   if(getResources().getConfiguration().orientation == getResources().getConfiguration().ORIENTATION_LANDSCAPE){
		   xMax = h-1;
		   yMax = w-1;
//		   groundLineY = xMax - (xMax /5) ;
	   }
	   else{
		   xMax = h-1;
		   yMax = w-1;
//		   groundLineY = yMax - (yMax /5) ;
	   }
     
     
   }
   private void drawBullet(Canvas canvas, float ballX2, float ballY2){
		
	   //I think ballX is ballY
	   
		 Log.v("LineRunner","  x2: "+ ballX2 +" Y2: "+ballY2 + "  : "+xMax );
		//draw a person
		 //head
		 paint.setColor(Color.BLUE);
		 canvas.drawCircle(ballX2+40, ballY2-(ballRadius * 2), ballRadius, paint);
   }
   private void drawRandomObstecal(Canvas canvas){
	   Random random = new Random();
	   int rNum = random.nextInt(101);
	   
	   /*
	    * Parameters RectF
left  The X coordinate of the left side of the rectangle 
top  The Y coordinate of the top of the rectangle 
right  The X coordinate of the right side of the rectangle 
bottom  The Y coordinate of the bottom of the rectangle  

	    */
	   RectF rBottom = new RectF(groundLineX-5, groundLineY+5, groundLineX+5, groundLineY);
	   RectF rTop = new RectF(groundLineX-5, groundLineY-27, groundLineX+5, groundLineY-20);
	   
	   Log.v("LineRunner:", "random roll: "+ rNum);
	   //draw a single cube
	   if(rNum  < 10){
		   canvas.drawRect(rBottom, paint);
		  
	   }
	   //draw a double cube
	   else if(rNum >= 11 && rNum <= 14 ){
		   canvas.drawRect(rBottom, paint);
		   groundLineX += 3;
		   canvas.drawRect(rBottom, paint);
	   }
	   //draw a triple cube
	   else if(rNum == 15){
		   canvas.drawRect(rBottom, paint);
		   groundLineX += 3;
		   canvas.drawRect(rBottom, paint);
		   groundLineX += 3;
		   canvas.drawRect(rBottom, paint);
	   }
	   //draw a single upper
	   else if(rNum >= 16 && rNum <= 18 ){
		   canvas.drawRect(rTop, paint);
		   
	   }
	   //draw a double upper
	   else if(rNum == 19){
		   canvas.drawRect(rTop, paint);
		   groundLineX += 3;
		   canvas.drawRect(rTop, paint);
		   
	   }
	   //draw single, again..
	   else{
		   //do noting..
	   }
	   
	   
	   
   }
   
   private void drawPerson(Canvas canvas, float ballX2, float ballY2){
		
	   //I think ballX is ballY
	   
		 Log.v("LineRunner","  x2: "+ ballX2 +" Y2: "+ballY2 + "  : "+xMax );
		//draw a person
		 //head
		 canvas.drawCircle(55, 40+ballX2, 6, paint);
		 //body
		 canvas.drawLine(53, 50+ballX2, 53, 100+ballX2, paint);
		 //straight arms
		 canvas.drawLine(53-25, 50+ballX2, 53+25, 50+ballX2, paint);
		 
		 //left leg
		 paint.setColor(Color.BLUE);
		 canvas.drawLine(53, 100+ballX2, 53-25, 150+ballX2, paint);
		 //right leg
		 paint.setColor(Color.RED);
		 canvas.drawLine(53, 100+ballX2, 53+25, 150+ballX2, paint);
	}
   private void drawPersonJump(){
	   YVel = -25;
   }
   
   private void drawPersonRoll(){
	   //would need to animate a rolling person
   }

@Override
public void onClick(View v) {
	drawPersonJump();
	
}
   
   
   
}